<!-- 供应商管理-供应商认证列表（管理）  -->
<template>
  <div>
    <pageHeader :title="title" :breadCrumb="breadCrumb" :show-back="true">
    </pageHeader>
    <div class="search-box">
      <a-space>
        <div>
          <a-input-group>
            <a-select
              :style="{ width: '120px' }"
              placeholder="请选择"
              v-model="currSelectVal"
            >
              <a-option value="number">供应商编号</a-option>
            </a-select>
            <a-input-search
              :style="{ width: 'auto' }"
              placeholder="请输入"
              v-model="currSelectSearch"
              search-button
              @search="search"
            />
          </a-input-group>
        </div>
        <!-- <div>
          <a-input-search
            :style="{ width: '200px' }"
            placeholder="请输入区域、行业搜索"
            search-button
          />
        </div> -->
      </a-space>
      <div>
        <a-space>
          <a-button type="outline" @click="reset">
            <template #icon>
              <icon-refresh />
            </template>
            <template #default>重置</template>
          </a-button>
          <a-button status="success">
            <template #icon>
              <icon-download />
            </template>
            <template #default>导入</template>
          </a-button>

          <a-button status="warning">
            <template #icon>
              <icon-export rotate="-90" />
            </template>
            <template #default>导出</template>
          </a-button>
        </a-space>
      </div>
    </div>
    <div style="padding: 10px">
      <tableTurn :tableData="getData" :args="args" ref="tableTurnRef">
        <a-table-column :width="80" align="center">
          <template #title> 序号 </template>
          <template #cell="{ rowIndex }">
            {{ rowIndex + 1 }}
          </template>
        </a-table-column>
        <a-table-column
          v-for="(item, index) in selectedHeader"
          :title="item.title"
          :key="index"
          :data-index="item.code"
          align="center"
          :width="200"
        >
          <template v-if="item.code === 'submitDemandTime'" #cell="{ record }">
            {{ dayjs(record.createdAt).format('YYYY-MM-DD') }}
          </template>
          <!-- 认证项目item -->
          <template v-else-if="item.code === 'supplierAuth'" #cell="{ record }">
            <a-space class="auntContent" direction="vertical" fill>
              <div
                class="aunt-item"
                v-for="element in record.supplierAuth"
                :key="element.id"
                :class="
                  element.state === 1
                    ? 'WaitSubmitted'
                    : element.state === 2
                    ? 'Qualified'
                    : element.state === 3
                    ? 'Examining'
                    : element.state === 4
                    ? 'Pass'
                    : 'NoPass'
                "
              >
                <div>
                  {{ element.authSetName }}
                </div>
              </div>
            </a-space>
          </template>
          <!-- 任务下单人item -->
          <template
            v-else-if="item.code === 'createUserName'"
            #cell="{ record }"
          >
            <a-space class="auntContent" direction="vertical" fill>
              <div
                class="item"
                v-for="element in record.supplierAuth"
                :key="element.id"
                >{{ element.headManName }}
              </div>
            </a-space>
          </template>

          <!-- 执行状态item -->
          <template
            v-else-if="item.code === 'ExecutionStatus'"
            #cell="{ record }"
          >
            <a-space class="auntContent" direction="vertical" fill>
              <div
                class="item"
                v-for="element in record.supplierAuth"
                :key="element.id"
              >
                <a-space>
                  <!-- <a-typography-text style="cursor: pointer" type="primary">
                    {{
                      element.state === 4
                        ? '通知整改'
                        : element.state === 5
                        ? '通知整改'
                        : ''
                    }}
                  </a-typography-text> -->

                  <a-typography-text
                    ref="authSetTypeText"
                    type="primary"
                    style="cursor: pointer"
                    @click="
                      jumps(
                        record,
                        element,
                        element.authSetName === '样品认证' ? 1 : 2
                      )
                    "
                  >
                    {{
                      element.state === 1
                        ? '任务下达'
                        : element.state === 2
                        ? '认证详情'
                        : element.state === 3
                        ? '认证详情'
                        : element.state === 4
                        ? '认证详情'
                        : element.state === 5
                        ? '认证详情'
                        : '任务下达'
                    }}
                  </a-typography-text>
                </a-space>
              </div>
            </a-space>
          </template>
        </a-table-column>

        <!-- 操作item -->
        <a-table-column align="center" :width="120" fixed="right" title="操作">
          <template #cell="{ record }">
            <!-- <a-button
              v-if="record.state !== 6"
              type="text"
              @click="goQualified(record)"
              >转合格供应商</a-button
            > -->
            <!-- <a-button type="text" @click="goDetail(record.id)"
              >档案详情</a-button
            >
            <a-popconfirm
              @ok="deleteItem(record.id)"
              content="确定删除吗"
              type="error"
            >
              <a-button
                type="text"
                :style="{
                  color: 'red',
                }"
                >删除</a-button
              >
            </a-popconfirm> -->

            <a-tooltip v-if="record.state !== 2" content="档案详情">
              <IconFont
                type="icon-details-def"
                :size="26"
                :style="{
                  marginRight: '10px',
                  cursor: 'pointer',
                }"
                @click="goDetail(record.id)"
              />
            </a-tooltip>
            <a-tooltip content="删除">
              <a-popconfirm
                @ok="deleteItem(record.id)"
                content="确定删除吗"
                type="error"
              >
                <IconFont
                  type="icon-delete"
                  :size="26"
                  :style="{
                    marginRight: '10px',
                    cursor: 'pointer',
                  }"
                />
              </a-popconfirm>
            </a-tooltip>
          </template>
        </a-table-column>
      </tableTurn>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { reactive, ref, onMounted, computed } from 'vue';
  import { useRouter } from 'vue-router';

  import { getAllIdentification } from '@/api/buy-management/supplier-management/supplier-certification/index';
  import {
    GetSupplierPrimaryList,
    DelSupplierPrimaryList,
    putSupplierPrimaryDetail,
  } from '@/api/buy-management/supplier-management/supplier-primary-list';
  import dayjs from 'dayjs';
  import { formHeaderSetting } from '@/types/global';

  const router = useRouter();
  const authSetTypeText = ref();
  const title = '供应商认证列表';
  const breadCrumb = ['采购管理', '供应商管理', '供应商认证列表'];

  const headerSetting = reactive<formHeaderSetting>({
    list: [],
  });

  const ModelVisible = ref(false);

  const forbidList = reactive(['salesName']); // 禁止移动的表头， (销售员)

  // 表格配置
  const args = reactive({
    stripe: true,
    bordered: {
      headerCell: true,
    },
    rowKey: 'name', // 批量选择按钮以客户名称来作为key
  });

  let tableTurnRef: any = ref(); // 储存table的ref
  let currSelectVal = ref(); // 储存当前下拉选择框的值
  let currSelectSearch = ref(''); // 储存下拉选择框对应的搜索框的值
  let currNameSearch = ref(''); // 储存搜索供应商名称的值
  let isFirstSelect = ref(true);

  // 获取列表数据
  const getData = async (pageData?: any) => {
    try {
      if (isFirstSelect.value) {
        const res = await getAllIdentification({
          ...pageData,
        });
        isFirstSelect.value = false;
        return Promise.resolve(res);
      } else {
        const res = await GetSupplierPrimaryList({
          ...pageData,
          name: currNameSearch.value,
          [currSelectVal.value]: currSelectSearch.value,
        });
        return Promise.resolve(res);
      }
    } catch (err: unknown) {
      console.log(err);
    }
  };

  // 搜索按钮
  const search = () => {
    tableTurnRef.value.loadData();
  };
  // 重置按钮
  const reset = () => {
    currSelectSearch.value = '';
    currNameSearch.value = '';
    currSelectVal.value = '';
    tableTurnRef.value.loadData();
  };

  const addautn = () => {
    ModelVisible.value = true;
  };

  // 监听键盘回车，按了回车也会查询
  const keyDown = (e: any) => {
    if (e.keyCode === 13) {
      tableTurnRef.value.loadData();
    }
  };

  onMounted(() => {
    // 监听键盘事件
    window.addEventListener('keydown', keyDown);
  });

  const jumps = (e, val, type) => {
    if (val.authSetType === 1 || val.authSetType === 3) {
      router.push({
        name: 'SupplierRegistrationSampletask',
        query: {
          gys: e.id,
          auth: val.id,
          type,
          state: val.state === 1 ? 'add' : 'edit',
        },
      });
    } else if (val.authSetType === 2) {
      router.push({
        name: 'FieldCertification',
        query: { gys: e.id, auth: val.id, type },
      });
    }
  };

  const columns = [
    {
      id: 2,
      title: '供应商编号',
      visible: true,
      type: 'string',
      code: 'number',
    },
    {
      id: 3,
      title: '供应商名称',
      visible: true,
      type: 'string',
      code: 'name',
    },
    {
      id: 4,
      title: '供应商等级',
      visible: true,
      type: 'string',
      code: 'supLevelName',
    },
    {
      id: 5,
      title: '供应商状态',
      visible: true,
      type: 'string',
      code: 'supCategoryName',
    },
    // {
    //   id: 6,
    //   title: '认证需求提交时间',
    //   visible: true,
    //   type: 'string',
    //   code: 'submitDemandTime',
    // },
    {
      id: 7,
      title: '认证需求提交人',
      visible: true,
      type: 'string',
      code: 'submitDemandUserName',
    },
    {
      id: 8,
      title: '认证项目',
      visible: true,
      type: 'string',
      code: 'supplierAuth',
    },
    {
      id: 9,
      title: '任务下单人',
      visible: true,
      type: 'string',
      code: 'createUserName',
    },
    {
      id: 10,
      title: '任务开始时间',
      visible: true,
      type: 'string',
      code: 'submitDemandTime',
    },
    {
      id: 11,
      title: '任务结束时间',
      visible: true,
      type: 'string',
      code: 'submitDemandTime',
    },
    {
      id: 12,
      title: '执行状态',
      visible: true,
      type: 'string',
      code: 'ExecutionStatus',
    },
  ];

  const selectedHeader = computed(() => {
    return columns.filter((item) => item.visible);
  });

  // 去往对应的详情页面
  const goDetail = (id: number) => {
    router.push({
      name: 'ArchivalInformation',
      query: {
        id,
        type: 2,
      },
    });
  };

  let currId = ref(-1); // 储存当前选中的id
  // 删除一项
  const deleteItem = async (id: number) => {
    try {
      await DelSupplierPrimaryList({
        id,
      });
      tableTurnRef.value.loadData();
    } catch (err: unknown) {
      console.log(err);
    }
  };

  const goQualified = async (record: any) => {
    let newArr = record;
    newArr.state = 6;
    await putSupplierPrimaryDetail(newArr);
  };
</script>

<style scoped lang="less">
  .search-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding: 0px 10px;
    margin-top: -7px;
  }

  :deep(.arco-space-item) {
    margin-top: 10px;
  }
  .auntContent {
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .aunt-item {
    width: 100px;
    height: 40px;
    line-height: 40px;
    border: 1px solid rgba(229, 230, 235);
    border-radius: 5px;
  }

  .item {
    width: 100px;
    height: 40px;
    line-height: 40px;
  }

  .WaitSubmitted {
    // 待下达
    background: #c9cdd4;
    color: rgb(255, 255, 255);
    border: none;
  }
  .Examining {
    // 执行中
    background: rgb(216, 229, 255);
    color: #165dff;
    border: none;
  }
  .Pass {
    // 已通过
    background-color: rgba(214, 243, 229);
    color: #31c27c;
    border: none;
  }
  .NoPass {
    //不通过
    background-color: rgb(255, 204, 204);
    border: none;
    color: #f53f3f;
  }
  .Qualified {
    // 待执行
    background: rgba(255, 247, 232);
    border: none;
    color: #ff9a2e;
  }
</style>
